import requests
import json





# 下载文档
comment_headers = {
    "X-Auth-Token": "daUnVbO1WzxhSiIo8bo2wkP2rKbDxjYpz0KD2Vy9",
    "Content-Type": "application/json",
    "X-Csrf-Token": "ouY6csTu-22dcyWS1M1vzyMS",
    "Referer": "https://se2024.yuque.com/org-wiki-se2024-go4lg1/kmyk33/pf0wupluuld8lho9",
    "Cookie": "lang=zh-cn; lang=zh-cn; _yuque_session=GjmDSBKgdOqFpJrwqFMTeWLU4LEwORq57qqJ7crdBU1MCD-jzhr9Yvf_T6TZ2yTtBfjg06Ftuz9fNFzNpxGJ_Q==; receive-cookie-deprecation=1; yuque_ctoken=ouY6csTu-22dcyWS1M1vzyMS; aliyungf_tc=81255cf344f0aaf1e715cf854371814804ed307fd930d9195a1fe70ee3d3a544; current_theme=default; acw_tc=ac11000117206336754746802e6df6c159ddd9de5fb189bfc8fa903aa98482"
}

comment_data = {
    "type": "word",
    "force": 0,
}
export_file_url = "https://se2024.yuque.com/api/docs/161258786/export"
export_response = requests.post(export_file_url, headers=comment_headers,data=json.dumps(comment_data))
# 根据export_response中的下载链接，将文件下载到本地
file_url = export_response.json()["data"]["url"]
file_response = requests.get(file_url)
with open("../export2.docx", "wb") as f:
    f.write(file_response.content)
# 讲export.docx转换为export.pdf
import os
from docx2pdf import convert

# 将.docx文件转换为.pdf文件
convert("../export2.docx", "export2.pdf")

# 检查文件是否成功转换
if os.path.exists("export2.pdf"):
    print("文件成功转换为PDF")
else:
    print("文件转换失败")
print(export_response.text)